ECE3730J RC3_LAB4
For this RC, we will go through Lab 4 in detail.
CubeMX
We need to generate PWM signal on PB14
(embeded with an LED D1
, or D2
if you like)
![[Pasted image 20240701151153.png]]
when the
PB14
outputLOW
,D1
will light up.
In STM32, Input Capture and Output Compare is configured in Timer channels.
![[Pasted image 20240701152054.png]]
Let’s check which Timer is connected to PB14
![[Pasted image 20240701152158.png]]
It’s the CH2N
channel of timer 1.
Then we configure timer 1.
![[Pasted image 20240701143248.png]]
![[Pasted image 20240701144606.png]]
Prescaler: 7200-1
Counter Period: 200-1 (50Hz)
Pulse: 180 (duty cycle of 90%; TIM_CNT 0179: HIGH, TIM_CNT 180199: LOW)
CHN Polarity: LOW (needed for CHN)
CHxN is the complementary channel of CHx in Timer1.
If the polarity of CHxN and CHx are different, the two channels will follow the same output pattern, otherwise they will be complementary
Also remember to configure PB12
(SW4
) as GPIO_Input.
![[Pasted image 20240701152725.png]]
You can try to use Input Capture to capture the press action of the button (as indicated in lab manual, i.e. connecting
PB12
toPA0
).
Code
Init PWM in main()
:
1 | /* USER CODE BEGIN 2 */ |
Change PWM duty cycle:
1 | void ChangeDutyCycle(int pulse_width) |
Get the press/release action of the button (PB12
):
1 | int pressed = 0; |